﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace BenevolentSun
{
    class PauseScreen : Screen
    {

        TextField title;

        Sprite background;

        Sprite logo;

        InstructionsControl instructionsControl;

        HorizontalMenuControl horizontalMenuControl;

        DimmerControl dimmerControl;


        public event EventHandler OnExitToMainMenu;

        public override void LoadContent()
        {
            SpriteFont titleFont = Content.Load<SpriteFont>("Fonts/Screen Title");
            title = new TextField("Paused", titleFont, new Color(255, 153, 0));
            title.AlignH = AlignH.Center;
            title.Position = new Vector2(1280f / 2f, 10f);
            title.Color.A = 0;

            logo = new Sprite(Content.Load<Texture2D>("Textures/Gui/Logo"));

            background = new Sprite(Content.Load<Texture2D>("Textures/Gui/Background"));

            instructionsControl = new InstructionsControl(Content);

            SpriteFont horizontalMenuFont = Content.Load<SpriteFont>("Fonts/Horizontal Menu Font");
            horizontalMenuControl = new HorizontalMenuControl(Content);
            horizontalMenuControl.AddEntry(new TextField("Resume", horizontalMenuFont, new Color(255, 0, 0)));
            horizontalMenuControl.AddEntry(new TextField("Options", horizontalMenuFont, new Color(0, 255, 0)));
            horizontalMenuControl.AddEntry(new TextField("Exit", horizontalMenuFont, new Color(255, 255, 0)));
            horizontalMenuControl.Initialize();
            horizontalMenuControl.FadeIn();

            dimmerControl = new DimmerControl(Content);
            dimmerControl.FadeIn();

            refreshMenuInstructions();

            base.LoadContent();
        }


        public override void ExitScreen()
        {
            horizontalMenuControl.FadeOut();
            instructionsControl.FadeOutInstructions();
            base.ExitScreen();
        }

        public override void HandleInput()
        {
            if (ActionMap.Cancel || ActionMap.Start)
            {
                ExitScreen();
            }
            else
            {
                if (ActionMap.Right)
                {
                    if (horizontalMenuControl.Next())
                    {
                        refreshMenuInstructions();
                        AudioManager.PlayCue("Pop");
                    }
                }

                if (ActionMap.Left)
                {
                    if (horizontalMenuControl.Prev())
                    {
                        refreshMenuInstructions();
                        AudioManager.PlayCue("Pop");
                    }
                }

                if (ActionMap.Confirm)
                {
                    if (horizontalMenuControl.Selected.Text == "Resume")
                    {
                        // open the level here
                        ExitScreen();
                    }
                    else if (horizontalMenuControl.Selected.Text == "Options")
                    {
                        OptionScreen optionScreen = new OptionScreen();
                        optionScreen.OnRemove += new EventHandler(optionScreen_OnRemove);
                        ScreenManager.AddScreen(optionScreen);
                        horizontalMenuControl.FadeOut();
                        horizontalMenuControl.AdvanceToEnd();
                    }
                    else if (horizontalMenuControl.Selected.Text == "Exit")
                    {
                        if (OnExitToMainMenu != null)
                            OnExitToMainMenu(this, EventArgs.Empty);
                        ExitScreen();
                    }
                }
            }

            base.HandleInput();
        }


        void optionScreen_OnRemove(object sender, EventArgs e)
        {
            ((OptionScreen)sender).OnRemove -= optionScreen_OnRemove;
            horizontalMenuControl.FadeIn();
        }


        private void refreshMenuInstructions()
        {
            if (horizontalMenuControl.Selected.Text == "Resume")          // start a new game
            {
                instructionsControl.HideInstructions();
                instructionsControl.SetInstructions("Return to the level and resume playing.");
                instructionsControl.FadeInInstructions();
            }
            else if (horizontalMenuControl.Selected.Text == "Options")
            {
                instructionsControl.HideInstructions();
                instructionsControl.SetInstructions("Switch the game options.");
                instructionsControl.FadeInInstructions();
            }
            else if (horizontalMenuControl.Selected.Text == "Exit")
            {
                instructionsControl.HideInstructions();
                instructionsControl.SetInstructions("Aw...Is this stage too tough for you? I guess you could return to the main menu.");
                instructionsControl.FadeInInstructions();
            }
        }


        public override void Update(GameTime gameTime, bool isFocusAvailable, bool isOccluded)
        {
            /*
            menu.Update(gameTime);
            */

            instructionsControl.Update(gameTime);
            horizontalMenuControl.Update(gameTime);
            dimmerControl.Update(gameTime);

            title.Color.A = (byte)horizontalMenuControl.FadeValue;

            base.Update(gameTime, isFocusAvailable, isOccluded);
        }


        public override void Draw(GameTime gameTime)
        {
            /*
            SpriteBatch.Begin();
            menu.Draw(SpriteBatch);
            SpriteBatch.End();
            */

            SpriteBatch.Begin();

            background.Draw(SpriteBatch);

            logo.Draw(SpriteBatch);

            dimmerControl.Draw(SpriteBatch);

            horizontalMenuControl.Draw(SpriteBatch);

            instructionsControl.Draw(SpriteBatch);

            title.Draw(SpriteBatch);

            SpriteBatch.End();

            base.Draw(gameTime);
        }

    }
}
